/*
 * Copyright (C) 2012 DataSymphony (http://datasymphony.com.au)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package au.com.ds.restty.cmd.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import au.com.ds.restty.cmd.ArgumentList;
import au.com.ds.restty.exc.InvalidInput;
import au.com.ds.restty.exc.LogicError;
import au.com.ds.restty.js.JsAnyObject;
import au.com.ds.restty.js.JsObjectFactory;
import au.com.ds.restty.js.JsObjectType;

public class ArgumentListImpl implements ArgumentList {
	private String[] mValues;
	private HashMap<String, JsAnyObject> mPreparedValues;
	private ArrayList<String> mNames;
	private ArrayList<String> mUndefinedNames;
	private int mArgsStartIndex;
	
	public ArgumentListImpl(String[] args, int argsStartIndex){
		mArgsStartIndex = argsStartIndex;
		if (args == null || args.length < mArgsStartIndex){
			throw new InvalidInput("Template name is not defined");
		}
		
		mValues = args;
	}

	@Override
	public void addDefinition(Map<String, JsAnyObject> def) {
		mPreparedValues = new HashMap<String, JsAnyObject>(def);
		mNames = new ArrayList<String>(def.keySet());
		for (int i = mArgsStartIndex; i < mValues.length; i++){
			String[] pair = mValues[i].split("=");
			if (pair.length == 2){
				// process by name
				if (!mPreparedValues.containsKey(pair[0])){
					throw new InvalidInput("Invalid argument name: " + pair[0]);
				} else {
					mPreparedValues.put(pair[0], JsObjectFactory.getJsString(pair[1]));
				}
			} else if (pair.length == 1){
				if ((i - mArgsStartIndex) >= mNames.size()){
					throw new InvalidInput("Unexpected argument: " + mValues[i]);
				}
				String name = mNames.get(i - mArgsStartIndex); // 0 - is for command name
				mPreparedValues.put(name, JsObjectFactory.getJsString(pair[0]));
			} else {
				throw new InvalidInput("Invalid argument format: " + mValues[i]);				
			}
		}
	}

	@Override
	public int getSize() {
		if (mNames == null){
			throw new LogicError("addDefinition should be called first");				
		}
		
		return mNames.size();
	}

	@Override
	public List<String> getNames() {
		if (mNames == null){
			throw new LogicError("addDefinition should be called first");				
		}
		
		return mNames;
	}

	@Override
	public JsAnyObject get(String name) {
		if (mNames == null){
			throw new LogicError("addDefinition should be called first");				
		}
		
		return mPreparedValues.get(name);
	}

	@Override
	public JsAnyObject get(int idx) {
		if (mNames == null){
			throw new LogicError("addDefinition should be called first");				
		}
		
		if (idx >= mNames.size()){
			throw new LogicError("Index out of bound: " + idx);				
		}

		return get(mNames.get(idx));
	}

	@Override
	public String getCommandName() {
		return mValues[mArgsStartIndex - 1];
	}

	@Override
	public List<String> getUndefinedNames() {
		if (mNames == null){
			throw new LogicError("addDefinition should be called first");				
		}
		
		if (mUndefinedNames == null){
			mUndefinedNames = new ArrayList<String>();
			for (Map.Entry<String, JsAnyObject> e : mPreparedValues.entrySet()){
				if (e.getValue().getType() == JsObjectType.UNDEFINED){
					mUndefinedNames.add(e.getKey());
				}
			}
		}
		
		return mUndefinedNames;
	}
}
